<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_Model extends CI_Model
{
    
    function __construct() {
        parent::__construct();
    } 
	
    // Begin List user
    function get_all_user($num,$offset,$arr_search)
    {
            /*Begin search*/
             if(!empty($arr_search)){
			   foreach($arr_search as $key=>$value){
				 if($value!='')
				 	$this->db->where("member.$key",$value);  
			   }
		   }
            /*End search*/

            //$this->db->where('bl_active',1);
             $this->db->order_by('member_id','DESC');
             $query = $this->db->get('member',$num,$offset);
             return $query->result();
    }

    function get_num_user($arr_search)
    {
            /*Begin search*/
             if(!empty($arr_search)){
			   foreach($arr_search as $key=>$value){
				 if($value!='')
				 	$this->db->where("member.$key",$value);  
			   }
		   }
            /*End search*/
            //$this->db->where('bl_active',1);
             $query = $this->db->get('member');
             return $query->num_rows();
    }


    //Save user
    function save_user($member_id,$data)
    {
             if($member_id!=0){
                      $this->db->where('member_id',$member_id);
                      if($this->db->update('member',$data)){
                                    return true;
                      }else{
                                    return false;
                      }
             }else{
                      if($this->db->insert('member',$data)){
                                    return true;
                      }else{
                                    return false;
                      }
             }
    }

    function get_list_login_code(){
             $this->db->order_by('lb_name','ASC');
             return $this->db->get('sys_login_code')->result();
    }   

    function del_user($id)
    {
             $this->db->select('member_id,lb_image');
             $this->db->where('member_id',$id);
             $check = $this->db->get('member')->row();
             if($check){
                      if(file_exists('../uploads/images/'.$check->lb_image)){
                                    //unlink('../uploads/images/'.$check->lb_image);
                      }
                      $this->db->where('member_id',$id);
                      if($this->db->delete('member')){
                                    return true;
                      }else{
                                    return false;
                      }
             }else{
                      return false;
             }
    }

    //Get member
    function get_user_item($member_id){
             $this->db->where('member_id',$member_id);
             $query = $this->db->get('member');
             return $query->row();
    }

    function get_user_check($lb_email,$id=0){
             $this->db->select('member_id');
             $this->db->where('lb_email ', trim($lb_email));
             $this->db->where('member_id !=', $id);
             $query = $this->db->get('member');
             return $query->row();
    }
    function get_parent(){
             return $this->db->get('fm_zone')->result();
    }

    function export_user($from='',$to='')
    {     
     if(!empty($from)) $this->db->where('dt_create', $from);
             if(!empty($to)) $this->db->where('dt_create', $to);

             $this->db->order_by('dt_create','DESC');
             $query = $this->db->get('member');
             return $query->result();
    }
}
?>
